from contextvars import copy_context
import pymysql
import cy_config
import time

db = pymysql.connect(
    host='',
    user='',
    password='',
    database='',
    port=3306
)

# 获取配置
def getConfigData(user_id, symbol):
    cursor = db.cursor()
    db.ping()

    search_sql = "select * from " + 'fa_wangge_config' + " where user_id=%s and symbol=%s"
    search_sql_value = (user_id, symbol)
    cursor.execute(search_sql, search_sql_value)
    datas = cursor.fetchall() # 获取查询数据

    db.commit() # 提交语句
    db.close()

    postion_config = {
        "symbol" : "",
        "more_by_u" : "0",
        "more_max_trad_amount" : '100000',
        "more_sell_increase" : '0.2',
        "more_interval_bl" : '0.2'
    }

    if datas:
        for item in datas:
            
            time.sleep(cy_config.min_sleep_amount)

            postion_config['user_id'] = item[0]
            postion_config['symbol'] = item[1]
            postion_config['more_by_u'] = item[3]
            postion_config['more_max_trad_amount'] = item[5]
            postion_config['more_sell_increase'] = item[7]
            postion_config['more_interval_bl'] = item[9]

        return postion_config

    return None

# 检测运行数据
def checkStatusData(user_id, symbol, open_type, item):
    # 先要检测是否存在对应的运行数据，如果存在那么就是更新，如果不存在那么就是上传
    cursor = db.cursor()
    db.ping()

    search_sql = "select * from " + 'fa_wangge_status' + " where user_id=%s and symbol=%s and status=%s"
    search_sql_value = (user_id, symbol, open_type)
    cursor.execute(search_sql, search_sql_value)
    datas = cursor.fetchall() # 获取查询数据

    db.commit() # 提交语句
    db.close()

    if datas:
        # 已经存在，进行更新
        uploadStatusDatas(user_id, symbol, open_type, item)
    else:
        # 不存在，进行上传
        insertStatusDatas(user_id, symbol, open_type, item)

# 更新运行数据
def uploadStatusDatas(user_id, symbol, open_type, item):
    cursor = db.cursor()
    db.ping()

    status = str(open_type)

    more_current_buyed_count = str(item['more_current_buyed_count'])
    more_max_trad_count = str(item['more_max_trad_count'])
    more_traed_count = str(item['more_traed_count'])
    more_roe_price = str(item['more_roe_price'])
    more_risk_amount = str(item['more_risk_amount'])
    more_current_interval = str(item['more_current_interval'])
    start_time = str(item['start_time'])
    end_time = str(item['end_time'])
    more_current_increase = str(item['more_current_increase'])
    more_buy_u = str(item['more_buy_u'])
    current_use_price = str(item['current_use_price'])
    org_price = str(item['org_price'])
    total_price = str(item['total_price'])
    start_price = str(item['start_price'])

    sql = "update " + 'fa_wangge_status' + " set"
    more_current_buyed_count_sql =  " more_current_buyed_count = %s,"
    more_max_trad_count_sql =  " more_max_trad_count = %s,"
    more_traed_count_sql =  " more_traed_count = %s,"
    more_roe_price_sql =  " more_roe_price = %s,"
    more_risk_amount_sql =  " more_risk_amount = %s,"
    more_current_interval_sql =  " more_current_interval = %s,"
    start_time_sql =  " start_time = %s,"
    end_time_sql =  " end_time = %s,"
    more_current_increase_sql =  " more_current_increase = %s,"
    more_buy_u_sql =  " more_buy_u = %s,"
    current_use_price_sql =  " current_use_price = %s,"
    org_price_sql =  " org_price = %s,"
    total_price_sql =  " total_price = %s,"
    start_price_sql =  " start_price = %s"

    total_sql = sql + more_current_buyed_count_sql + more_max_trad_count_sql + more_traed_count_sql + more_roe_price_sql + more_risk_amount_sql + more_current_interval_sql + start_time_sql + end_time_sql + more_current_increase_sql + more_buy_u_sql + current_use_price_sql + org_price_sql + total_price_sql + start_price_sql
    total_sql = total_sql + " WHERE user_id=%s and symbol=%s and status=%s"

    update_value = (more_current_buyed_count, more_max_trad_count, more_traed_count, more_roe_price, more_risk_amount, more_current_interval, start_time, end_time, more_current_increase, more_buy_u, current_use_price, org_price, total_price, start_price, user_id, symbol, status)

    cursor.execute(total_sql, update_value)

    db.commit() # 提交语句
    db.close()
    pass

# 上传运行数据
def insertStatusDatas(user_id, symbol, open_type, item):
    cursor = db.cursor()
    db.ping()

    more_current_buyed_count = item['more_current_buyed_count']
    # less_current_buyed_count = item['less_current_buyed_count']
    more_max_trad_count = item['more_max_trad_count']
    # less_max_trad_count = item['less_max_trad_count']
    more_traed_count = item['more_traed_count']
    # less_traed_count = item['less_traed_count']
    more_roe_price = item['more_roe_price']
    # less_roe_price = item['less_roe_price']
    more_risk_amount = item['more_risk_amount']
    # less_risk_amount = item['less_risk_amount']
    more_current_interval = item['more_current_interval']
    # less_current_interval = item['less_current_interval']
    start_time = item['start_time']
    end_time = item['end_time']
    more_current_increase = item['more_current_increase']
    # less_current_increase = item['less_current_increase']
    more_buy_u = item['more_buy_u']
    # less_buy_u = item['less_buy_u']
    current_use_price = item['current_use_price']
    org_price = item['org_price']
    total_price = item['total_price']
    start_price = item['start_price']

    # 只传多 空不弄

    sql = "insert into fa_wangge_status(user_id, symbol, status, more_current_buyed_count, more_max_trad_count, more_traed_count, more_roe_price, more_risk_amount, more_current_interval, start_time, end_time, more_current_increase, more_buy_u, current_use_price, org_price, total_price, start_price) values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    cursor.execute(sql, (user_id, symbol, open_type, more_current_buyed_count, more_max_trad_count, more_traed_count, more_roe_price, more_risk_amount, more_current_interval, start_time, end_time, more_current_increase, more_buy_u, current_use_price, org_price, total_price, start_price))

    db.commit() # 提交语句
    db.close()